<?php
class omeanalysts_mdl_ome_orderpromotion extends dbeav_model{

    //var $has_export_cnf = true;

    var $export_name = '促销订单查询表';

    public function table_name($real=false){
        $table_name = 'ome_delivery';
        if($real){
            return kernel::database()->prefix.$table_name;
        }else{
            return $table_name;
        }
    }

    public function count($filter=null){
        $where = $this->_filter($filter); 

        $sql = "SELECT os.order_bn,f.shop_bn,f.`name` shop_name,f.AXCustomer,f.customername,os.createtime,os.total_amount,os.payed,its.bn,its.`name`,its.nums,its.price,its.divide_order_fee,its.newdivide_order_fee from sdb_ome_orders os 
        inner join sdb_ome_order_items its on os.order_id=its.order_id
        inner join sdb_ome_shop f on os.shop_id=f.shop_id
        where ".$where." and f.shop_bn='T196' and os.is_promotion='true' and its.`delete`='false' ";
        $row = $this->db->select($sql);
        return count($row);


    }

    public function getlist($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null){
  

        if(!$filter['time_from'] && !$filter['time_to'])
        {
            return false;
        }else{

            $where = $this->_filter($filter); 
          $sql = "SELECT os.order_bn,f.shop_bn,f.`name` shop_name,f.AXCustomer,f.customername,os.createtime,os.total_amount,os.payed,its.bn,its.`name`,its.nums,its.price,its.divide_order_fee,its.newdivide_order_fee from sdb_ome_orders os 
          inner join sdb_ome_order_items its on os.order_id=its.order_id
          inner join sdb_ome_shop f on os.shop_id=f.shop_id
          where ".$where." and f.shop_bn='T196' and os.is_promotion='true' and its.`delete`='false' ";
                     
            $rows = $this->db->selectLimit($sql,$limit,$offset);
            
            return $rows;
        }
        
    }
    
    public function searchOptions(){
        $parentOptions = parent::searchOptions();
        $childOptions = array();
        return $Options = array_merge($parentOptions,$childOptions);
    }

    public function _filter($filter,$tableAlias=null,$baseWhere=null)
    {
//店铺、客户、订单号、发货单号、物流单号、物料编码

        $where = array(1);
       if(empty($filter['time_from']))
        {
          $where[]=' 1=2 ';
        }
        if(isset($filter['time_from']) && $filter['time_from']){
            $where[] = ' os.createtime >='.strtotime($filter['time_from']);
        }
        if(isset($filter['time_to']) && $filter['time_to']){
            $where[] = ' os.createtime <'.(strtotime($filter['time_to'])+86399);
        } 
         if(isset($filter['order_bn']) && $filter['order_bn']){
            $where[]= ' os.order_bn  =\''.addslashes($filter['order_bn']).'\'';

        }                     
        if(isset($filter['p_bn']) && $filter['p_bn']){
            $where[] = ' its.bn =\''.addslashes($filter['p_bn']).'\'';
        }

        return implode($where,' AND ');
    }

    public function get_schema() 
    {
      //订单号、店铺、客户编码、客户名称、订单时间、发货单号、物流单号、物料、单位、数量、是否暂停、是否校验、是否打印、是否备货
        $schema = array (
            'columns' => array (
               'order_bn' =>
                array(
                    'type' => 'varchar(32)',
                    'required' => true,
                    'label' => '订单号',
                    'is_title' => true,
                    'width' => 125,
                    'searchtype' => 'nequal',
                    'editable' => false,
                    'filtertype' => 'normal',
                    'filterdefault' => true,
                    'in_list' => true,
                    'default_in_list' => false,
                ),
                 'shop_bn' =>
                array (
                  'type' => 'varchar(255)',
                  'required' => true,
                  'label' => '店铺编码',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ),
                 'shop_name' =>
                array (
                  'type' => 'varchar(255)',
                  'required' => true,
                  'label' => '店铺名称',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ),
                'AXCustomer' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => '客户编码',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ),
                 'customername' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => '客户名称',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ),
               'createtime' =>
                array (
                'type' => 'time',
                'label' => '订单时间',
                'comment' => '订单时间',
                'editable' => false,
                'in_list' => true,
                ),
                'total_amount' =>
                array (
                  'type' => 'money',
                  'required' => true,
                  'label' => '订单总额',
                  'editable' => false,
                  'width' =>140,
                  'filtertype' => 'yes',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                ),
                'bn' =>
                array (
                  'type' => 'varchar(30)',
                  'label' => '物料',
                  'editable' => false,
                  'is_title' => true,
                ),
                'name' =>
                array (
                  'type' => 'varchar(200)',
                  'required' => true,
                  'label' => '物料名称',
                  'is_title' => true,
                  'default_in_list' => true,
                  'width' => 70,
                  'searchtype' => 'has',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                ),
                'nums' =>
                array (
                  'type' => 'number',
                  'label' => '数量',
                  'required' => true,
                  'default' => 0,
                  'editable' => false,
                ),
                'price' =>
                array (
                  'type' => 'money',
                  'required' => true,
                  'label' => '原价',
                  'editable' => false,
                  'width' =>140,
                  'filtertype' => 'yes',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                ),
                'divide_order_fee' =>
                array (
                  'type' => 'money',
                  'required' => true,
                  'label' => '实付金额',
                  'editable' => false,
                  'width' =>140,
                  'filtertype' => 'yes',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                ),
                'newdivide_order_fee' =>
                array (
                  'type' => 'money',
                  'required' => true,
                  'label' => '前实付金额',
                  'editable' => false,
                  'width' =>140,
                  'filtertype' => 'yes',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                ),
            ),

            'idColumn' => 'name',
            'in_list' => array (
                0 => 'order_bn',
                1 => 'shop_bn',
                2 => 'shop_name',
                3 => 'AXCustomer',
                4 => 'customername',
                5 => 'createtime',
                6 => 'total_amount',
                7 => 'bn',
                8 => 'name',
                9 => 'nums',
                10 => 'price',
                11 => 'divide_order_fee',
                12 => 'newdivide_order_fee',
            ),
            'default_in_list' => array (
                0 => 'order_bn',
                1 => 'shop_bn',
                2 => 'shop_name',
                3 => 'AXCustomer',
                4 => 'customername',
                5 => 'createtime',
                6 => 'total_amount',
                7 => 'bn',
                8 => 'name',
                9 => 'nums',
                10 => 'price',
                11 => 'divide_order_fee',
                12 => 'newdivide_order_fee',
            ),

        );
        return $schema;
    }

    /**
     * 获得日志类型(non-PHPdoc)
     * @see dbeav_model::getLogType()
     */
    public function getLogType($logParams) {
        $type = $logParams['type'];
        $logType = 'none';
        if ($type == 'export') {
            $logType = $this->exportLogType($logParams);
        }
        elseif ($type == 'import') {
            $logType = $this->importLogType($logParams);
        }
        return $logType;
    }
    /**
     * 导出日志类型
     * @param Array $logParams 日志参数
     */
    public function exportLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_export';
        return $type;
    }
    /**
     * 导入操作日志类型
     * @param Array $logParams 日志参数
     */
    public function importLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_import';
        return $type;
    }

    function io_title( $filter, $ioType='csv' )
    {
        switch( $filter ){
            case 'delivery':
                $this->oSchema['csv'][$filter] = array(
                    '*:订单号' => 'order_bn',
                    '*:店铺编码' => 'shop_bn',
                    '*:店铺名称' => 'shop_name',
                    '*:客户编码' => 'AXCustomer',
                    '*:客户名称' => 'customername',
                    '*:订单时间' => 'createtime',
                    '*:订单总额' => 'total_amount',
                    '*:物料' => 'bn',
                    '*:物料名称' => 'name',
                    '*:数量' => 'nums',
                    '*:原价' => 'price',
                    '*:实付金额' => 'divide_order_fee',
                    '*:前实付金额' => 'newdivide_order_fee',
                );
                break;
        }
        $this->ioTitle[$ioType][$filter] = array_keys( $this->oSchema[$ioType][$filter] );
        return $this->ioTitle[$ioType][$filter];
    }

    //根据查询条件获取导出数据
    public function getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end){
          //根据选择的字段定义导出的第一行标题
          if($curr_sheet == 1){

              $title = array();
              foreach($this->io_title('delivery') as $k => $v ){
                  $title[] = mb_convert_encoding($v, 'GBK', 'UTF-8');
              }

              $data['content']['main'][0] = '"'.implode('","',$title).'"';
          }

          $where = $this->_filter($filter);
          $sql = "SELECT os.order_bn,f.shop_bn,f.`name` shop_name,f.AXCustomer,f.customername,FROM_UNIXTIME(os.createtime, '%Y-%m-%d %H:%i:%S') as createtime ,os.total_amount,its.bn,its.`name`,its.nums,
          its.price,its.divide_order_fee,its.newdivide_order_fee from sdb_ome_orders os 
          inner join sdb_ome_order_items its on os.order_id=its.order_id
          inner join sdb_ome_shop f on os.shop_id=f.shop_id
          where ".$where." and f.shop_bn='T196' and os.is_promotion='true' and its.`delete`='false' ";       
          error_log(date('Y-m-d H:i:s').'发送数据/'.var_export($sql, true), 3, DATA_DIR . '/orderpromotion_' . date('Y-m-d') . '.log');
            $omedelivery = $this->db->selectLimit($sql,$end,$start);
            foreach ($omedelivery as $k => $aFilter) {
                $exptmp_data = array();
                foreach ($aFilter as $col => $val) {
                    $aFilter[$col] = mb_convert_encoding($aFilter[$col], 'GBK', 'UTF-8');
                    $exptmp_data[] = $aFilter[$col];
                }

                $data['content']['main'][] = implode(',', $exptmp_data);
            }
            return $data;
        
    }
}
